﻿@using GameStepAzure.Models;
@using EntityModel = GameStepAzure.Areas.Admin.Models;
<div class="shopping_cart">
    <div class="cart_title">
        Carrito
    </div>
    <div class="cart_details">
        @{            
            List<ProductoCarrito> lst = null;

            // Usuario autentificado
            MembershipUser usr = Membership.GetUser();
            if (usr != null)
            {
                using (EntityModel.GameStoreEntities context = new EntityModel.GameStoreEntities())
                {
                    Guid uid = new Guid(Convert.ToString(usr.ProviderUserKey));
                    List<EntityModel.ProductoCarrito> entities = context.ProductoCarrito.ToList().FindAll(o => o.user_id.Equals(uid));

                    lst = new List<ProductoCarrito>(entities.Count);

                    foreach (EntityModel.ProductoCarrito pc in entities)
                    {
                        lst.Add(EntityModel.GameStoreMapper.ProductoCarritoEntityToModel(pc));
                    }
                }
            }
            // Usuario anonimo
            else
            {
                if (Session["carrito"] != null)
                {
                    lst = (List<ProductoCarrito>)Session["carrito"];
                }
            }

            float monto = 0;
            int itemsTotal = 0;
            if (lst != null)
            {
                monto = (from p in lst select p.Producto.Precio * p.Cantidad).Sum();

                foreach (ProductoCarrito p in lst)
                {
                    itemsTotal += p.Cantidad;
                }
            }
    
            @itemsTotal
            if (itemsTotal == 1)
            {
            <span>Art&iacute;culo</span>
            }
            else
            {
            <span>Art&iacute;culos</span>
            }
            <br />    
            <span>Total:&nbsp;</span><span class="price">$@monto</span>    
        }
    </div>
    <div class="cart_icon">
        <a href="@Url.Action("Index", "Carrito", null)" title="header=[Ver Carrito] body=[&nbsp;] fade=[on]">
            <img src="/Images/GameStore/shoppingcart.png" alt="" title="" width="48" height="48" /></a></div>
</div>
